<!DOCTYPE html>
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="../node_modules/vue/dist/vue.global.js"></script>
    <!-- 引入axios -->
    <script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
</head>

<body>
    <div id="app">
        <p>
            姓: <input type="text" v-model="userinfo.firstname">
        </p>
        <p>
            名: <input type="text" v-model="userinfo.lastname">
        </p>
        <p>
            姓名: <input type="text" v-model="userinfo.fullname">
        </p>
        <button @click="changeUserinfo">修改userinfo</button>

        <!-- 使用计算属性监听userinfo -->
        <p>计算属性-userinfo: {{comUserinfo}}</p>

    </div>
</body>

</html>
<script>


    // 侦听器watch 默认只能监听一级数据, 或者说只能监听基本数据类型数据,引用数据类型监听不到  
    // 注意: 深层监听器初始化还是不触发
    const { createApp } = Vue;
    const vm = createApp({
        data() {
            return {
                // firstname: '',
                // lastname: '',
                // fullname: '',
                userinfo: {
                    firstname: '',
                    lastname: '',
                    fullname: ''
                }
            }
        },
        // 计算属性:
        computed: {
            comUserinfo() {
                console.log('计算属性-userinfo');
                return this.userinfo.firstname + this.userinfo.lastname
            }
        },
        methods: {
            changeUserinfo() {
                // 整个userinfo 被修改
                this.userinfo = {
                    firstname: '郭',
                    lastname: '',
                    fullname: ''
                }
            }
        },
        watch: {
            // userinfo(newvalue, oldvalue) {
            //     console.log(newvalue, oldvalue);

            // },
            // 深度监听器
            userinfo: {
                handler(newvalue, oldvalue) {
                    console.log('watch');
                    // 注意：在嵌套的变更中，
                    // 只要没有替换对象本身，
                    // 那么这里的 `newValue` 和 `oldValue` 相同
                    // console.log(newvalue, oldvalue);
                    newvalue.fullname = newvalue.firstname + newvalue.lastname
                },
                deep: true,
                immediate: true
            }

        }
    }).mount('#app')


    // 使用axios 进行数据的请求
    // axios({
    //     method: 'get',
    //     url: 'https://api.i-lynn.cn/college',
    // }).then(function (response) {
    //     console.log('response', response);
    // });

</script>